package com.example.demo.EmailTest.util;

import org.springframework.stereotype.Component;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

@Component
public class EmailUtil {

    /**
     * 发送邮件
     *
     * @param map 参数
     * @param myEmailName 发件人姓名
     * @param emailTitle 标题
     * @param content 内容
     *
     * @throws Exception
     */
    public static void sendMail(Map<String, Object> map, String myEmailName, String emailTitle, String content) throws Exception {

        String myEmailAccount = "FF9981521@163.com";
        //myEmailPassword:SMTP 客户端设置了独立密码,这里的邮箱密码必需使用这个独立密码（授权码）
        String myEmailPassword = "KDLDXKKNPWYLVNFV";
        String myEmailSMTPHost = "smtp.163.com";
        String smtpPort = "465";

        // 参数配置
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");   // 使用的协议（JavaMail规范要求）
        props.setProperty("mail.smtp.host", myEmailSMTPHost);   // 发件人的邮箱的 SMTP 服务器地址
        props.setProperty("mail.smtp.auth", "true");            // 需要请求认证
        //如果部署linux需要开启以下四个配置,如果win项目这不需要(阿里云禁止25端口,本地不禁,开启465端口同时需要开启SSL配置)
        props.setProperty("mail.smtp.port", smtpPort);   // 云服务器默认25端口,云服务器封禁了25端口，代码改用465
        props.setProperty("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.socketFactory.port", smtpPort);

        //创建会话对象, 用于和邮件服务器交互
        Session session = Session.getDefaultInstance(props);
        session.setDebug(true);                                 // 设置为debug模式, 可以查看详细的发送 log

        MimeMessage message = createMimeMessage(session,map, myEmailAccount,myEmailName,emailTitle,content);

        Transport transport = session.getTransport();

        transport.connect(myEmailAccount, myEmailPassword);
        transport.sendMessage(message, message.getAllRecipients());
        // 关闭连接
        transport.close();
        System.out.println("邮件发送完成,收件人:"+map.get("recipientName").toString());
    }

    /**
     * 创建一封只包含文本的简单邮件
     *
     * @param session 和服务器交互的会话
     * @param sendMail 发件人邮箱
     * @param content 内容
     * @param myEmailName 发件人姓名
     * @param emailTitle 标题
     * @return
     * @throws Exception
     */
    public static MimeMessage createMimeMessage(Session session, Map<String, Object> map, String sendMail, String myEmailName, String emailTitle, String content) throws Exception {
        // 1. 创建一封邮件
        MimeMessage message = new MimeMessage(session);

        // 2. From: 发件人
        message.setFrom(new InternetAddress(sendMail, myEmailName, "UTF-8"));

        // 3. To: 收件人（可以增加多个收件人、抄送、密送）
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(map.get("recipientEmail").toString(), map.get("recipientName").toString(), "UTF-8"));

        // 4. Subject: 邮件主题
        message.setSubject(emailTitle, "UTF-8");

        // 5. Content: 邮件正文（可以使用html标签）
        message.setContent(content, "text/html;charset=UTF-8");

        // 6. 设置发件时间
        message.setSentDate(new Date());

        // 7. 保存设置
        message.saveChanges();

        return message;
    }
}
